//
//  YZAvatarVC.m
//  VET
//
//  Created by Lam BG on 2018/7/3.
//  Copyright © 2018年 YZ. All rights reserved.
//

#import "YZAvatarVC.h"
#import "YZProfessionalInfoVC.h"

#import "YZUser.h"

#import "YZAvatarVM.h"

#import "YZGradientBtn.h"

#import "SDWebImageManager.h"
#import "UIViewController+YZImagePicker.h"

@interface YZAvatarVC ()

@property (weak, nonatomic) IBOutlet UITextField *verCodeTF;

@property (weak, nonatomic) IBOutlet UIView *verCodeView;

@property (weak, nonatomic) IBOutlet UIButton *chooseImgBtn;

@property (weak, nonatomic) IBOutlet YZGradientBtn *nextBtn;

@property (nonatomic, strong) YZAvatarVM *avatarVM;

@end

@implementation YZAvatarVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)setup {
    [super setup];
    
    self.title = @"基本资料";
    
    
    self.navigationItem.hidesBackButton = YES;
    self.chooseImgBtn.layer.cornerRadius = self.chooseImgBtn.frame.size.width / 2;
    self.chooseImgBtn.layer.masksToBounds = YES;
    
    
    // 有头像就显示头像
    NSString *avatarUrl = [YZUser sharedInstance].avatar;
    if (avatarUrl.length) {
        
        // 下载显示
        SDWebImageManager *manager = [SDWebImageManager sharedManager];
        [manager loadImageWithURL:[NSURL URLWithString:avatarUrl] options:0 progress:^(NSInteger receivedSize, NSInteger expectedSize, NSURL * _Nullable targetURL) {
            
        } completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, SDImageCacheType cacheType, BOOL finished, NSURL * _Nullable imageURL) {
            
            if (image) {
                // do something with image
                [self.chooseImgBtn setImage:image forState:UIControlStateNormal];
                self.avatarVM.isUploadComplete = finished;
            }
        }];
    }
}

- (void)bindVM {
    
    RAC(self.avatarVM, userName) = self.verCodeTF.rac_textSignal;
    @weakify(self)
    [self.verCodeTF.rac_textSignal subscribeNext:^(NSString *input) {

        @strongify(self)
        NSInteger kMaxLength = 5;
        NSString *lang = [[UIApplication sharedApplication]textInputMode].primaryLanguage;
        if ([lang isEqualToString:@"zh-Hans"]) {
            
            // 中文输入
            UITextRange *selectedRange = [self.verCodeTF markedTextRange];
            // 获取高亮部分
            UITextPosition *position = [self.verCodeTF positionFromPosition:selectedRange.start offset:0];
            // 没有高亮选择的字，则对已输入的文字进行字数统计和限制
            if (!position) {
                if (input.length > kMaxLength) {

                    self.verCodeTF.text = [input substringToIndex:kMaxLength];
                }
            } else {
                //有高亮选择的字符串，则暂不对文字进行统计和限制
            }
        } else {
            
            // 不考虑限制
            if (input.length > kMaxLength) {
                
                self.verCodeTF.text = [input substringToIndex:kMaxLength];
            }
        }
    }];
    
    RAC(self.nextBtn, enabled) = self.avatarVM.nextSignal;
    [[self.nextBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
        
        @strongify(self)
        [self.avatarVM.nextCmd execute:nil];
    }];
    [[self.avatarVM.nextCmd executionSignals] subscribeNext:^(RACSignal<id> *signal) {
        
        [signal subscribeNext:^(NSString *uid) {
            
            @strongify(self)
            // 更新完成后跳转
            YZProfessionalInfoVC *vc = [[UIStoryboard storyboardWithName:@"personalCenter" bundle:nil] instantiateViewControllerWithIdentifier:@"YZProfessionalInfoVC"];
            vc.needRightItem = YES;
            [self.navigationController pushViewController:vc animated:YES];
        }];
    }];
}

#pragma mark - event actions

- (IBAction)chooseImgClicked:(id)sender {
 
    [self choosePicture];
}

#pragma mark - UIImagePickerController Delegate

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    
    UIImage *image = [self imageFromPickerVC:picker gotPicWithDicInfo:info];
    if (image) {
        
        [self.chooseImgBtn setImage:image forState:UIControlStateNormal];
        [[self.avatarVM uploadSignal:image withID:@"1179856204409749504"] subscribeNext:^(NSDictionary *json) {
            
            if (json != nil) {
                if (json[@"progress"]) {
                    
                    // 当前进度
                    
                } else if (json[@"data"]) {
                    
                    // 上传完成
                }
            }
        } completed:^{
            
            // 上传完成
        }];
    }
}

#pragma mark - getter 懒加载

- (YZAvatarVM *)avatarVM {
    
    if (_avatarVM == nil) {
        
        _avatarVM = [[YZAvatarVM alloc] init];
    }
    
    return _avatarVM;
}

@end
